/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:WebSocketMessageServiceImpl.java   2020-08-18 21:45 wanglijun
 */
package cn.datacare.marmot.service.impl;

import cn.datacare.marmot.bo.MessageVo;
import cn.datacare.marmot.bo.PosOrderCollect;
import cn.datacare.marmot.service.WebSocketMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-08-18 21:45
 */
@Slf4j
@Service
public class WebSocketMessageServiceImpl implements WebSocketMessageService, InitializingBean {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    /**
     *
     */
    private List<PosOrderCollect> posOrderCollects=new ArrayList<>();

    private Random random1 = new Random(100);


    private BigDecimal  num4=new BigDecimal(100);

    @Override
    public void afterPropertiesSet() throws Exception {
        PosOrderCollect posOrderTotal=new PosOrderCollect();
        posOrderTotal.setColor("#2d8cf0");
        posOrderTotal.setIcon("md-cart");
        posOrderTotal.setTitle("总数据量");
        posOrderTotal.setCount(803);
        posOrderTotal.setPercent(new BigDecimal(2.60));
        posOrderCollects.add(posOrderTotal);

        posOrderTotal=new PosOrderCollect();
        posOrderTotal.setColor("#19be6b");
        posOrderTotal.setIcon("md-checkmark-circle");
        posOrderTotal.setTitle("订单数据量(成功)");
        posOrderTotal.setCount(232);
        posOrderCollects.add(posOrderTotal);

        posOrderTotal=new PosOrderCollect();
        posOrderTotal.setColor("#ff9900");
        posOrderTotal.setIcon("md-alert");
        posOrderTotal.setTitle("订单数据量(失败)");
        posOrderTotal.setCount(42);
        posOrderCollects.add(posOrderTotal);

        posOrderTotal=new PosOrderCollect();
        posOrderTotal.setColor("#2db7f5");
        posOrderTotal.setIcon("md-analytics");
        posOrderTotal.setTitle("平均峰值");
        posOrderTotal.setCount(657);
        posOrderCollects.add(posOrderTotal);

    }

    //@Scheduled(fixedRate = 10000)
    @Override
    public void sendTopicMessage() {
        log.info("广播推送消息");
        MessageVo messageVo=new MessageVo();
        messageVo.setAge(17);
        messageVo.setRealName("Evan");
        messageVo.setUsername("scwanglijun");
       this.messagingTemplate.convertAndSend("/topic/message",messageVo);
    }

    @Scheduled(fixedRate = 3000)
    @Override
    public void sendPosOrder() {
        log.info("POS_ORDER_广播推送消息");

        for(PosOrderCollect posOrderCollect:this.posOrderCollects){
            int num= (int) (Math.random()*30);
            if(posOrderCollect.getTitle().equals("总数据量")){
                    int num2= (int) (Math.random()*1000);
                    BigDecimal num3=new BigDecimal(num2);
                    posOrderCollect.setPercent(num3.divide(num4).setScale(2, BigDecimal.ROUND_DOWN));
            }
            posOrderCollect.setCount(posOrderCollect.getCount()+num);
        }

        this.messagingTemplate.convertAndSend("/topic/posOrder",this.posOrderCollects);
    }
}
